/*
 Creator: Kisamin Resident and copyright by her.  All rights reserved except those specifically set forth here:
 You may:
 	 Use these scripts in any project
	 Modify these scripts in any way you wish subject to:
	 		1. Retaining this set of comments
			2. Explicity commenting and showing any changes you might make
 You may NOT:
 	 License these scripts to someone else with restrictions differing from the above.
	 Charge a fee for the scripts themselves (although you may charge for an object that
	 		utilizes these scripts to add functionality)
	 Charge a fee for any "library" or "collection" of scripts that includes this script.

If you have questions about your rights under this coment and/or the Gnu CopyLeft, please request further information
and/or explicit permission from Kisamin Resident
*/



integer	k_iCmdAnnounce	= 10000;			// Command code for link messages
integer	k_iCmdExplode	= 10001;			// Tell the warhead to explode
integer k_iCmdNewTarget = 10002;			// We've locked on to a new victim
integer k_iCmdSelfDestruct = 10003;			// We need to self -destruct either due to time or some other function.
integer	k_iCmdKill = 11000;					// Tell a damage script to kill someone

float	k_fFuseDelay = 0.0;				// How long after launch the warhead goes active (different than sensor active!)
float	k_fCloseEnough = 5.0;			// How close is "close enough" for the warhead to explode?

integer k_iIgnoreOwner = TRUE;			// Should we ignore the owner in our collisions and sensors?
integer k_iIgnoreGroup = FALSE;			// Should we ignore things owned by our group in collisions and sensors?

float	k_fExplosionRadius = 10.0;

/* ==================================================================
	Shouldn't need to change anything below this
	=================================================================
*/

integer	g_iIsExploding = FALSE;
integer	g_iNumDamageScripts;

key		g_kOwner;

default {
	state_entry () {
		
		integer i;
		
		for (i=0; i<llGetInventoryNumber (INVENTORY_SCRIPT); i++) {
			if (llSubStringIndex (llToLower (llGetInventoryName (INVENTORY_SCRIPT, i)), "DamageCore") == 0)
				g_iNumDamageScripts++;
		}
	}
	
	on_rez (integer in_iParam) {
		if (in_iParam) {
			llResetTime ();
			llSensorRepeat ("", NULL_KEY, AGENT, PI, k_fCloseEnough, 0.01);
			g_kOwner = llGetOwner ();
		} else {
			g_kOwner = llGetOwner ();
			llSensorRemove ();
			llMessageLinked (LINK_SET, k_iCmdAnnounce, "If you change the number of scripts in the missile, please reset the warhead script.", g_kOwner);
		}
	}
	
	sensor (integer in_iNumSensed) {
		integer i;
		
		if (g_iIsExploding) {
			for (i=0; i<in_iNumSensed; i++)
				llMessageLinked (LINK_THIS, k_iCmdKill + (i%g_iNumDamageScripts), "", llDetectedKey (i));
			return;
		}
		
		if (!k_iIgnoreOwner && !k_iIgnoreGroup) {
			g_iIsExploding = TRUE;
			llSensorRemove ();
			llSensor ("", NULL_KEY, AGENT, k_fExplosionRadius, PI);
			return;
		}
			
		for (i=0; i<in_iNumSensed; i++) {
			if (
				(! k_iIgnoreOwner || (llDetectedKey (i) != llGetOwner ()))
				&& (!k_iIgnoreGroup || !llDetectedGroup (i))
				) {
					g_iIsExploding = TRUE;
					llSensorRemove ();
					llSensor ("", NULL_KEY, AGENT, k_fExplosionRadius, PI);
				}
		}
	}
			
	link_message (integer in_iFromPrim, integer in_iCommand, string in_sData, key in_kKey) {
		if (in_iCommand != k_iCmdExplode)
			return;
		
		// This is where the actual explosion code should go.
		llSensorRemove ();
		llSensor ("", NULL_KEY, AGENT, k_fExplosionRadius, PI);
	}
	
	collision_start (integer in_iNumCollisions) {
		integer i;
		
		for (i=0; i<in_iNumCollisions; i++) {
			if ((!k_iIgnoreOwner || (llDetectedKey (i) != llGetOwner ()))
				&& (!k_iIgnoreGroup || !llDetectedGroup (i))) {
					g_iIsExploding = TRUE;
					llSensorRemove ();
					llSensor ("", NULL_KEY, AGENT, k_fExplosionRadius, PI);
			}
		}
	}
	
	land_collision (vector in_vCollisionPoint) {
		g_iIsExploding = TRUE;
		llSensorRemove ();
		llSensor ("", NULL_KEY, AGENT, k_fExplosionRadius, PI);
	}
}
